<template>
  <el-dialog
    class="plan-dialog"
    :title="this.id ? '修改': '添加'"
    :visible.sync="value"
    :before-close="cancel"
  >
    <el-row :gutter="10">
      <el-form :model="dialogForm" :rules="rules" ref="dialogForm" label-width="100px">
        <el-col :span="12">
          <el-form-item label="计划名称" prop="name">
            <el-input v-model="dialogForm.name" placeholder="请输入任务名称" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划分组" prop="categroy">
            <el-input v-model="dialogForm.categroy" placeholder="请输入任务分组" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="关联任务" prop="jobkey" size="small">
            <el-select style="width:100%;" v-model="dialogForm.jobkey" placeholder="请选择任务">
              <el-option
                v-for="item in jobs"
                :key="item.id"
                :label="item.name"
                :value="JSON.stringify({group: item.categroy,name: item.id})"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划类型" size="small">
            <el-select style="width:100%;" v-model="dialogForm.type" placeholder="请选择计划类型">
              <el-option
                v-for="item in types"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="dialogForm.type !== 2">
          <el-form-item label="间隔时间" size="small">
            <el-input-number style="width:100%;" v-model="dialogForm.seconds" :min="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="dialogForm.type !== 2">
          <el-form-item label="循环次数" size="small">
            <el-input-number style="width:100%;" v-model="dialogForm.forever" :min="-1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="dialogForm.type === 2">
          <el-form-item label="cron表达式" size="small">
            <el-input v-model="dialogForm.cron" placeholder="请输入cron表达式"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" size="small">
            <div class="block">
              <el-date-picker v-model="dialogForm.startTime" value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择计划开始时间"></el-date-picker>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" size="small">
            <div class="block">
              <el-date-picker v-model="dialogForm.endTime" value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择计划结束时间"></el-date-picker>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="计划状态" prop="descript" size="small">
            <el-switch
              v-model="dialogForm.status"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949"
            ></el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="计划描述" prop="descript">
            <el-input type="textarea" placeholder="请输入计划描述" v-model="dialogForm.descript"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div slot="footer">
      <el-button @click="cancel" size="small">取 消</el-button>
      <el-button type="primary" @click="submit" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { list, addPlan, updatePlan } from "@/api/task";
export default {
  name: "planAddAndUpddate",
  components: {},
  props: {
    value: Boolean
  },
  data() {
    return {
      id: null,
      dialogForm: {
        name: "",
        categroy: "",
        jobkey: "",
        job: null,
        type: 0,
        descript: "",
        startTime: null,
        endTime: null,
        forever: -1,
        seconds: 1,
        status: 0
      },
      jobs: [],
      types: [
        //0-简单,1-日历，2-cron表达式，3-其他
        { label: "简单", value: 0 },
        { label: "日历", value: 1 },
        { label: "cron表达式", value: 2 }
      ],
      rules: {
        name: [
          { required: true, message: "计划名称不能为空", trigger: "blur" }
        ],
        categroy: [
          { required: true, message: "计划分组不能为空", trigger: "blur" },
          {
            required: true,
            validator: (rule, value, callback) => {
              const regx = /[\u4e00-\u9fa5]/gu;
              if (regx.test(value)) {
                callback(new Error("不能输入中文"));
              }
              callback();
            }
          }
        ],
        jobkey: [{ required: true, message: "任务不能为空", trigger: "blur" }]
      }
    };
  },
  computed: {},
  mounted() {
    this.listJobs();
  },
  methods: {
    async listJobs() {
      const { data } = await list();
      this.jobs = data;
    },
    init(row) {
      this.dialogForm = row;
    },
    submit() {
      this.$refs["dialogForm"].validate(async isOk => {
        if (isOk) {
          this.dialogForm.job = JSON.parse(this.dialogForm.jobkey);
          this.dialogForm.formTime = this.dialogForm.startTime
          this.dialogForm.toTime = this.dialogForm.endTime
          if (this.dialogForm.id) {
            await updatePlan(this.dialogForm);
          } else {
            await addPlan(this.dialogForm);
          }
          // 重新刷新数据
          this.$emit("refresh");
        }
      });
    },
    cancel() {
      this.$refs["dialogForm"].resetFields();
      this.dialogForm = {
        name: "",
        categroy: "",
        clazz: ""
      };
      this.$emit("input", false);
    },
    handleCurrentChange(val) {
        console.log(1,val);
    }
  }
};
</script>

<style lang="scss">
.plan-dialog .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}

.el-select-dropdown__wrap {
  margin-bottom: 0px !important;
}
</style>